package com.neusoft.elm.interfaces.controller;

import com.neusoft.elm.application.service.UserServiceImpl;
import com.neusoft.elm.domain.entity.User;
import com.neusoft.elm.domain.service.UserService;
import com.neusoft.elm.infrastructure.web.Result;
import javax.servlet.http.HttpServletRequest;

public class UserController {
    private final UserService userService;

    public UserController() {
        this.userService = new UserServiceImpl();
    }

    public Result<User> login(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        
        if (userId == null || password == null) {
            return Result.error("用户名和密码不能为空");
        }
        
        try {
            User user = userService.login(userId, password);
            return Result.success(user);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    public Result<Integer> register(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String password = request.getParameter("password");
        String userName = request.getParameter("userName");
        String userSex = request.getParameter("userSex");
        String userImg = request.getParameter("userImg");
        
        if (userId == null || password == null || userName == null) {
            return Result.error("必填信息不能为空");
        }
        
        User user = new User();
        user.setUserId(userId);
        user.setPassword(password);
        user.setUserName(userName);
        user.setUserSex(userSex != null ? Integer.parseInt(userSex) : 1);
        user.setUserImg(userImg);
        
        try {
            int result = userService.register(user);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    public Result<Boolean> checkUserId(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        
        if (userId == null) {
            return Result.error("用户ID不能为空");
        }
        
        try {
            boolean exists = userService.checkUserId(userId);
            return Result.success(exists);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
} 